package com.dcits.exception;

import com.dcits.common.Result;
import io.jsonwebtoken.JwtException;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    // 业务异常：人工定义
    @ExceptionHandler(BusinessException.class)
    public Result<Object> handleBusinessEx(BusinessException ex) {
        log.error("<业务异常>", ex);
        return Result.fail(ex.getCode(), ex.getMessage());
    }

    // 业务异常：参数校验
    @ExceptionHandler(ParamException.class)
    public Result<Object> handleParamEx(ParamException ex) {
        return Result.fail(ex.getCode(), ex.getMessage());
    }

    // 所有未捕获异常
    @ExceptionHandler(Exception.class)
    public Result<Object> handleAllEx(Exception ex) {
        log.error("<系统内部异常>", ex);
        return Result.fail("999999", ex.getMessage());
    }

    @ExceptionHandler(UnauthorizedException.class)
    public Result handleUnauthorizedException(UnauthorizedException e) {
        return Result.fail("403000", "没有访问权限");
    }

    @ExceptionHandler(AuthorizationException.class)
    public Result handleAuthorizationException(AuthorizationException e) {
        return Result.fail("403000", "权限验证失败");
    }

    @ExceptionHandler(JwtException.class)
    public Result handleJwtException(JwtException e) {
        return Result.fail("403000", "权限验证失败");
    }

    @ExceptionHandler(AuthenticationException.class)
    public Result handleJwtException(AuthenticationException e) {
        return Result.fail("403000", "权限验证失败");
    }

}